﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace AnaViewer
{
    public partial class OneAnaPanel : UserControl
    {

        AnaSubject subject;
        public AnaSubject Subject
        {
            get { return subject; }
            set
            {
                subject = value;
                if (subject != null)
                {
                    subject.SelectedIndexChanged += new EventHandler(subject_SelectedIndexChanged);
                }
            }
        }

        void subject_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetDisplayItem(subject.SelectedItem);
        }
        private IItem displayItem = null;
        public IItem DisplayItem
        {
            get { return displayItem; }
        }
        /// <summary>
        /// 显示一个 IItem 。
        /// </summary>
        /// <param name="item"></param>
        void SetDisplayItem(IItem value)
        {
            this.displayItem = value;
            string title = "";
            string desc = "";
            string url = "";
            string name = "";
            string time = "";
            Image face = null;

            string native = "";
            string metier = "";
            if (displayItem != null)
            {
                title = displayItem.Title;
                desc = displayItem.Description;
                time = displayItem.PubDate;
                url = displayItem.Link;
                name = displayItem.Author;
                face = displayItem.FaceImage;

                if (displayItem.GetType() == typeof(AnaItem))
                {
                    AnaItem item = displayItem as AnaItem;
                    if (item != null && item.Person != null)
                    {
                        Person person = item.Person;

                        if (person.Name == null || person.Name.Trim() == "")
                            name = "佚名";
                        else
                            name = person.Name;

                        if (person.Sex != null && person.Sex.Trim() != "")
                            name += " (" + person.Sex.Trim() + ")";

                        native = person.NativePlace;
                        metier = person.Metier;
                    }
                }
            }
            if (url == null || url.Trim() == "")
                linkGotoView.Visible = false;
            else
                linkGotoView.Visible = true;


            if (title == null || title.Trim() == "")
                title = "(无标题)";
            if (desc == null || desc.Trim() == "")
                desc = "(无内容)";
            if (name == null || name.Trim() == "")
                name = "佚名";
            if (time == null || time.Trim() == "")
                time = "(不详)";
            if (face == null)
                face = Properties.Resources.person45;
            if (native == null || native.Trim() == "")
                native = "(不详)";
            if (metier == null || metier.Trim() == "")
                metier = "(不详)";

            labelTitle.Text = title;
            rtbDesc.Text = desc;

            labelCreated.Text = time;

            pictureBox1.Image = face;
            labelAuthorName.Text = name;
            labelNativePlace.Text = native;
            labelMetier.Text = metier;



        }

        public OneAnaPanel()
        {
            InitializeComponent();

            linkGotoView.Click += new EventHandler(linkGotoView_Click);


            SetDisplayItem(null);
        }

        void linkGotoView_Click(object sender, EventArgs e)
        {
            if (displayItem == null || displayItem.Link == null || displayItem.Link.Trim() == "")
                return;

            Common.GoUrl(displayItem.Link);
        }
    }
}
